﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using palmApp.Session;
using palmApp.Database;

namespace palmApp
{
    public partial class maintenanceScreen : Form
    {
        mobileSession mbSession = mobileSession.Instance;
        Int32 comtec_id_machine;

        public maintenanceScreen(Int32 _comtec_id_machine)
        {
            InitializeComponent();

            comtec_id_machine = _comtec_id_machine;

            responsibleTextBox.Text = mbSession.name + " " + mbSession.lastname1;

            this.ShowDialog();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuItem4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuItem8_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuItem10_Click(object sender, EventArgs e)
        {
            
        }

        private void menuItem7_Click(object sender, EventArgs e)
        {
            statusMaintComboBox.SelectedIndex = -1;
            operationTypeComboBox.SelectedIndex = -1;
            diagTextBox.Text = "";
            diagTextBox.Text = "";
            changedPartsTextBox.Text = "";
            periodMantTextBox.Text = "";
            itemsEnabler(true);
        }

        private void menuItem9_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void menuItem6_Click(object sender, EventArgs e)
        {
            Stack<Object> data = new Stack<Object>();
            data.Push(operationTypeComboBox.SelectedIndex);
            data.Push(DateTime.Now);
            data.Push(1); // status operation
            data.Push(diagTextBox.Text);
            data.Push(changedPartsTextBox.Text);
            data.Push(comtec_id_machine);
            data.Push(mbSession.comtec_id_responsible);
            data.Push(nextMaintainmentDate.Value);
            data.Push(periodMantComboBox.SelectedItem);
            data.Push(periodMantTextBox.Text);
            data.Push(statusMaintComboBox.SelectedIndex);
            
            Manager maintenanceMan = new Manager();
            Int32 result = maintenanceMan.NewOperation(data.ToArray());
            maintenanceMan.Close();

            // itemsEnabler(false);
            
            if (result != -1)
            {
                DialogResult dlgRes = MessageBox.Show(
                    string.Format("¡Los datos se han guardado satisfactoriamente!"),
                    "Nueva operación",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation,
                    MessageBoxDefaultButton.Button1
                    );
                if (dlgRes == DialogResult.OK)
                {
                    this.Close();
                }
            }
        }

        /*
         * 
        private Boolean verifyIfTextBoxAreEmpty()
        {
            Boolean status = false;
            if (responsibleTextBox.Text.Length == 0 &&
                maintCodTextBox.Text.Length == 0 &&
                diagTextBox.Text.Length == 0 &&
                changedPartsTextBox.Text.Length == 0 &&
                periodMantTextBox.Text.Length == 0
                )
                status = true;
            return (status);
        }
        */

        private void itemsEnabler(bool status)
        {
            statusMaintComboBox.Enabled = status;
            operationTypeComboBox.Enabled = status;
            diagTextBox.Enabled = status;
            changedPartsTextBox.Enabled = status;
            periodMantTextBox.Enabled = status;
            nextMaintainmentDate.Enabled = status;
        }

        private void menuItem11_Click(object sender, EventArgs e)
        {
            itemsEnabler(true);
        }

        private void menuItem12_Click(object sender, EventArgs e)
        {
            menuItem7_Click(sender, e);
        }

        private void label3_ParentChanged(object sender, EventArgs e)
        {

        }

        private void tabPage3_Click(object sender, EventArgs e)
        {

        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void menuItem3_Click(object sender, EventArgs e)
        {

        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            historyScreen history = new historyScreen(comtec_id_machine);
        }
    }
}